package com.platform.controller.web;


import com.google.common.collect.Lists;
import com.platform.common.annotation.Log;
import com.platform.common.core.domain.AjaxResult;
import com.platform.common.core.domain.model.WebLoginUser;
import com.platform.common.core.page.TableDataInfo;
import com.platform.common.enums.BusinessType;
import com.platform.common.enums.ResultEnum;
import com.platform.common.exception.BusinessException;
import com.platform.common.utils.SecurityUtils;
import com.platform.system.domain.dto.device.DeviceInfoDto;
import com.platform.system.domain.vo.device.DeviceInfoVo;
import com.platform.system.domain.vo.device.HumanModelManagerVo;
import com.platform.system.logic.DeviceInfoLogic;
import com.platform.system.service.ISysDeviceInfoService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author Huangkai
 */
@Slf4j
@RestController
@RequestMapping("/device/web")
public class WebDeviceInfoController {

    @Resource
    ISysDeviceInfoService deviceInfoService;

    /**
     * 查询用户绑定的设备列表
     */
    @Log(title = "通过客户端uid查询绑定的设备列表", businessType = BusinessType.OTHER)
    @PostMapping("/selectDeviceInfoDtoByUserId")
    public AjaxResult selectDeviceInfoDtoByUserId(@RequestBody @Validated DeviceInfoVo deviceInfoVo) {
        WebLoginUser webLoginUser = SecurityUtils.getWebLoginUser();
        deviceInfoVo.setWebUserUid(webLoginUser.getUid());
        List<DeviceInfoDto> deviceInfoDtoList =  deviceInfoService.selectDeviceInfoDtoByUserId(deviceInfoVo);
        return AjaxResult.success(deviceInfoDtoList);
    }

    /**
     * 修改
     */
    @Log(title = "查询设备信息", businessType = BusinessType.UPDATE)
    @PostMapping("/selectDeviceInfoByMac")
    public AjaxResult selectDeviceInfoByMac(@RequestBody @Validated DeviceInfoVo deviceInfoVo) {
        DeviceInfoDto deviceInfoDto = deviceInfoService.selectDeviceInfoByMac(deviceInfoVo);
        if(deviceInfoDto == null){
            throw new BusinessException(ResultEnum.DATA_NOT_EXIST);
        }
        return AjaxResult.success(deviceInfoDto);
    }

    /**
     * 小程序修改默认的设备
     */
    @Log(title = "小程序修改默认的设备", businessType = BusinessType.UPDATE)
    @PostMapping("/setDefaultUserDevice")
    public AjaxResult setDefaultUserDevice(@RequestBody @Validated DeviceInfoVo deviceInfoVo) {
        WebLoginUser webLoginUser = SecurityUtils.getWebLoginUser();
        deviceInfoVo.setWebUserUid(webLoginUser.getUid());
        return deviceInfoService.setDefaultUserDevice(deviceInfoVo);
    }


    /**
     * 修改
     */
    @Log(title = "小程序修改设备信息", businessType = BusinessType.UPDATE)
    @PostMapping("/updateDeviceInfo")
    public AjaxResult updateDeviceInfo(@RequestBody @Validated DeviceInfoVo deviceInfoVo) {
        return deviceInfoService.updateDeviceInfo(deviceInfoVo);
    }

    /**
     * 解绑
     */
    @Log(title = "解绑", businessType = BusinessType.DELETE)
    @PostMapping("/deleteDeviceInfo")
    public AjaxResult deleteDeviceInfoList(@RequestBody @Validated DeviceInfoVo deviceInfoVo) {
        deviceInfoVo.setUidList(Lists.newArrayList(deviceInfoVo.getUid()));
        return deviceInfoService.deleteDeviceInfoList(deviceInfoVo);
    }

    /**
     * 解绑
     */
    @Log(title = "激活设备", businessType = BusinessType.OTHER)
    @PostMapping("/activateDevice")
    public AjaxResult activateDevice(@RequestBody @Validated DeviceInfoVo deviceInfoVo) {
        return deviceInfoService.activateDevice(deviceInfoVo);
    }

    /**
     * 查询租户可以使用的配置信息
     */
    @Log(title = "查询租户可以使用的配置信息", businessType = BusinessType.OTHER)
    @PostMapping("/selectTenantAuthAiByDeviceInfo")
    public TableDataInfo selectTenantAuthAiByDeviceInfo(@RequestBody @Validated DeviceInfoVo deviceInfoVo) {
        return deviceInfoService.selectTenantAuthAiByDeviceInfo(deviceInfoVo);
    }

}
